package com.console.front.support;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.console.front.model.MovsModel;

public class HttpUtils {

	/*
	 * 获取指定URL的html内容
	 */
	public static Document getPageContent(String url){
		try {
			Connection connection = Jsoup.connect(url);
			connection.timeout(10 * 1000);
			Document doc = connection.ignoreContentType(true).get();
			return doc;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<MovsModel> paseDocument(Document doc){
		List<MovsModel> models = new ArrayList<MovsModel>();
		org.jsoup.nodes.Element rootElement = doc.getElementById("data").getElementById("wide_col").getElementsByClass("section").get(0).getElementsByClass("list_videos").get(0);
		Elements itemsElement = rootElement.getElementsByClass("item");
		for(int i=0;i<itemsElement.size();i++){
			org.jsoup.nodes.Element item = itemsElement.get(i);
			String url = item.getElementsByTag("a").attr("href");
			String imgUrl = item.getElementsByTag("a").first().getElementsByTag("img").attr("src");
			String name = item.getElementsByTag("a").first().getElementsByTag("img").attr("alt");
			
			MovsModel model = new MovsModel();
			model.name = name;
			model.thumb_url = imgUrl;
			model.mov_url = url;
			
			models.add(model);
		}
		return models;
	}
	
}
